﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace TiecCuoi
{
    public partial class DoiPass : Form
    {
        bool Flag = false;
        //Data base
        private SqlConnection _CNN;
        public DoiPass(SqlConnection cnn)
        {
            _CNN = cnn;
            InitializeComponent();
        }

        private void B_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void B_OK_Click(object sender, EventArgs e)
        {
            if (TB_MKmoi.Text.Trim() != TB_MKmoi2.Text.Trim())
            {
                MessageBox.Show("Mật khẩu nhập lại không đúng", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                TB_MKmoi.Text = "";
                TB_MKmoi2.Text = "";
            }
            else
            {
                try
                {
                    string ham = "select MatKhau from Taikhoan where MatKhau='" + TB_MKcu.Text.Trim() + "'";
                    SqlCommand select = new SqlCommand(ham, _CNN);
                    SqlDataReader read = select.ExecuteReader();
                    if (read.Read())
                    {
                        Flag = true;
                    }
                    else
                    {
                        MessageBox.Show(" Mật khẩu sai", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        Flag = false;
                        TB_MKcu.Text = "";
                        TB_MKmoi.Text = "";
                        TB_MKmoi2.Text = "";
                    }
                    read.Close();
                    select.Dispose();
                    read.Dispose();
                }
                catch (Exception e1)
                {
                    MessageBox.Show(e1.Message);
                }
            }
            if (Flag)
            {
                try
                {
                    string ham = "Update  Taikhoan set MatKhau = '" + TB_MKmoi.Text.Trim() + "'";
                    SqlCommand select = new SqlCommand(ham, _CNN);
                    SqlDataReader read = select.ExecuteReader();
                    read.Close();
                    select.Dispose();
                    read.Dispose();
                    MessageBox.Show("Đổi mật khẩu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.Close();
                }
                catch (Exception e1)
                {
                    MessageBox.Show(e1.Message);
                    MessageBox.Show("Đổi mật khẩu thất bại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    TB_MKcu.Text = "";
                    TB_MKmoi.Text = "";
                    TB_MKmoi2.Text = "";

                }
            }
        }
    }
}
